/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ejb.metier;

import com.ejb.persistence.Address;
import com.ejb.persistence.Author;
import com.ejb.persistence.Book;
import com.ejb.persistence.BookCategory;
import com.ejb.persistence.Invoice;
import com.ejb.persistence.Order;
import com.ejb.persistence.User;
import com.ejb.persistence.UserAuthentication;
import com.ejb.persistence.UserType;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 * Bean used to test the persitence layer with JUnit.
 * @author Julien
 */
@Stateless
@LocalBean
public class TestBean {
    @PersistenceContext
    EntityManager em;
    
    public String test = "test";
    public void presistenceTestInsertBook(){
        
        Book book = new Book();
        book.setTitle("Test");
        book.setPrice(15f);
        book.setStock(10);
        book.setTitle(test);
        book.setPublicationDate(new Date());
        book.setEditor("fuck");
        book.setCategory(BookCategory.Fantasy);
        em.persist(book);
        ArrayList<Book> b = new ArrayList<Book>();
        b.add(book);
        Author author = new Author();
        author.setFirstName("fuck");
        author.setLastName("fuck");
        author.setBooks(b);
        em.persist(author);
    }
    
//    public void persistenceTestInsertUser(){
//        
//        User user = new User();
//        user.setFirstName("Jean");
//        user.setLastName("Valjean");
//        user.setEmail("jv1qsj@gmdsail.com");
//        user.setType(UserType.Utilisateur);
//        Address address = new Address();
//        address.setCity("Aix");
//        address.setCountry("France");
//        address.setLine1("1600 rte des milles");
//        address.setLine2("");
//        address.setZipCode("13090");
//        address.setUser(user);
//        user.setAddress(address);
//        UserAuthentication userAuth = new UserAuthentication();
//        userAuth.setLogin("jvsdj85ds3");
//        userAuth.setPassword("jvjcode");
//        user.setUserAuth(userAuth);
//        Order order = new Order();
//        Invoice invoice = new Invoice();
//        order.setUser(user);
//        em.persist(address);
//        em.persist(user);
//        em.persist(order);
//        //System.out.println(address.getId() + " " + user.getId() + " " + order.getId());
//        invoice.setBook(em.find(Book.class, 2l));
//        invoice.setOrders(order);
//        invoice.setQuantity(3);
//        em.persist(invoice);
//    }
    
//    public List<Book> mostSoldBooks(){
//        Query q = em.createNamedQuery("Invoice.findMostSoldBook");
//        q.setMaxResults(10);
//        List<Object[]> list = q.getResultList();
//        List<Book> retour = new ArrayList<Book>();
//        
//        for (Object[] o: list){
//            retour.add((Book)o[0]);
//        }
//        return retour;
//    }

}
